// pages/address/address.js
let utils = require("../../utils/utils.js");
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		username: '请输入绑定姓名',
		phone: "请输入绑定手机号码",
		gaddress: "请输入公司地址",
		gname: "请输入公司名称",
		email: '2665644567@qq.com',
		birthday: '请选择出生日期',
		telephonenumber: '29292929',
		isDisabled: false,
		isComplete: false,
		buttonTitle: "完成",
		updateAddr: '',
		/* 提交按钮事件 */
		submit: '',
		date: '2016-09-01',
		getUserInfo: 'getUserInfo',
		k7: [{
				'arr_guige02': '男',
				'guige_key02': 0,
				'checked': true
			},
			{
				'arr_guige02': '女',
				'guige_key02': 1,
				'checked': false
			},
		],
		sex: '男',
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo')
	},
	radio: function(e) {
		this.setData({
			guige_key02: e.currentTarget.dataset.id
		})
	},
	// 发货地址选择,获取用户选择的单选框的值
	radioChange: function(e) {
		this.data.sex = e.detail.value;
	},
	bindDateChange(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value);
		this.data.birthday = e.detail.value;
		this.setData({
			birthday: e.detail.value
		})
	},
	/* 绑定个人信息 */
	formSubmit(e) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		console.log(userInfo);
		//let birthday=this.data.birthday;
		//const addrInfo = e.detail.value;
		let addrInfo = {
			username: e.detail.value.username,
			phone: e.detail.value.phone,
			birthday: '',
			sex: '',
			gname: e.detail.value.gname,
			gaddress: e.detail.value.gaddress
		};
		addrInfo.sex = this.data.sex;
		addrInfo.birthday = this.data.birthday;
		if (userInfo.avatarUrl == '') {
			wx.showModal({
				title: "提示",
				content: "请授权获取个人信息",
				showCancel: false
			});
		} else if (addrInfo.username == "" || addrInfo.phone == "请选择出生日期" || addrInfo.birthday == "" || addrInfo.gname ==
			"" || addrInfo.gaddress ==
			"") {
			this.data.isComplete = false;
			wx.showModal({
				title: "提示",
				content: "请完善信息",
				showCancel: false
			});
		} else if (!/^[1][3,4,5,7,8]\d{9}$/.test(addrInfo.phone)) {
			wx.showModal({
				title: "提示",
				content: "手机号格式不规范",
				showCancel: false
			});
		} else {
			this.data.isComplete = true;
			wx.setStorageSync('addrInfo', addrInfo);
			that.updateAddr();
			return false;
		}
		this.setData({
			isDisabled: true,
			isComplete: this.data.isComplete
			// buttonTitle: "修改"
		});
	},
	updateAddr(e) {
		let that = this;
		//let userInfo = app.userInfo;
		let userInfo = wx.getStorageSync('userInfo');
		let addrInfo = wx.getStorageSync('addrInfo');
		console.log(addrInfo)
		let base_url = app.globalData.baseurl;
		let url = '/base/bind';
		console.log(userInfo,url, addrInfo);
		let data = {
			'imageUrl': userInfo.avatarUrl,
			'wechatName': userInfo.nickName,
			'openId': userInfo.openId,
			'username': addrInfo.username,
			'sex': addrInfo.sex,
			'birthday': addrInfo.birthday,
			'phoneNumber': addrInfo.phone,
			'companyName': addrInfo.gname,
			'companyAdress': addrInfo.gaddress
		}
		utils.request(userInfo, url, 'post', data, res => {
			console.log(res)
			if (res.data.status == 200||res.data.status == 200) {
					let jsonuserinfo=res.data.data;
					userInfo.userId = jsonuserinfo.id;
					wx.setStorageSync('userInfo', userInfo);
					wx.navigateTo({
						'url': '../index/index'
					})
			} else if(res.data.status == 501){
				utils.login(res => {
					console.log(res);
					let jsonuserinfo = res.data.data;
					
					userInfo.openId = jsonuserinfo.openId;
					userInfo.headerValue = jsonuserinfo.sessionValue;
					userInfo.userId = jsonuserinfo.userId;
					wx.setStorageSync('userInfo', userInfo);
					that.updateAddr();
					
				});
			}
			return false;
		})

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		console.log(userInfo);
		//if(userInfo.)



	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
